#include "SoundManager.h"
#include "ExplosionReverbFilter.h"
#include <conio.h>

int main(int argc, char** argv)
{
	ALfloat zeros[]  = {0.0, 0.0, 0.0},
			ones[]   = {1.0, 1.0, 0.0},
			twos[]   = {2.0, 2.0, 0.0},
			threes[] = {3.0, 3.0, 0.0};

	std::map<const char*, float> cullMap;
	cullMap["one"]   = 1.0f;
	cullMap["two"]   = 2.0f;
	cullMap["three"] = 3.0f;

	printf("init... ");
	SoundManager* soundManager = SoundManager::instancePtr();
	soundManager->init(1.0f);
	soundManager->enableDistanceCulling(&cullMap);
	printf("done!\n");
	printf("distance culling: %i\n", soundManager->isDistanceCulling());

	soundManager->setListenerProperties(zeros, zeros, zeros);
	printf("load music... ");
	Sound* music = soundManager->loadMusic("sound/music.ogg", true);
	//soundManager->playStreaming("sound/music.ogg", 5, "three", zeros, zeros, false);
	printf("done!\n");

	printf("load sounds... ");
	SoundBuffer sound1b = soundManager->loadSound("sound/da.wav"),
				sound2b = soundManager->loadSound("sound/tank_explode_1.wav"),
				sound3b = soundManager->loadSound("sound/tank_hit_2.wav");
	printf("done!\n");
	printf("---------------------------------------------\n");
	
	system("pause");
	
	Sound* sound1s;
	Sound* sound2s;
	Sound* sound3s;
	int delay = 10e2;

	soundManager->playMusic();
	soundManager->setMusicVolume(0.1f);

	sound1s = soundManager->playSound(sound1b, 3, "three", twos, twos, true);
	for (int i = 0; i < 3*delay; i++)
	{
		soundManager->update();
		printf("pause1-1");
	}
	sound2s = soundManager->playSound(sound2b, 3, "three", twos, twos, true);
	for (int i = 0; i < 3*delay; i++)
	{
		soundManager->update();
		printf("pause1-1");
	}
	sound3s = soundManager->playSound(sound3b, 3, "three", twos, twos, true);
	for (int i = 0; i < 3*delay; i++)
	{
		soundManager->update();
		printf("pause1-1");
	}

	sound1s = soundManager->playSound(sound1b, 3, "three", twos, twos, true);
	sound2s = soundManager->playSound(sound2b, 3, "three", twos, twos, true);
	sound3s = soundManager->playSound(sound3b, 3, "three", twos, twos, true);
	soundManager->pauseSounds();

	SFXFilter* filter = new ExplosionReverbFilter;

	//soundManager->enableMasterSFXFilter(filter, NO_TIME_LIMIT);
	//soundManager->enableSoundsSFXFilter(filter, 1);
	//soundManager->enableMusicSFXFilter(filter, 2);

	if (sound1s)
		sound1s->resume();
	for (int i = 0; i < delay; i++)
	{
		soundManager->update();
		printf("pause1-1");
	}
	if (sound2s)
		sound2s->resume();
	for (int i = 0; i < delay; i++)
	{
		soundManager->update();
		printf("pause1-1");
	}
	if (sound3s)
		sound3s->resume();
	for (int i = 0; i < 3*delay; i++)
	{
		soundManager->update();
		printf("pause1-1");
	}

/**
	soundManager->playMusic();
	soundManager->playStreaming("sound/intro.ogg", 1, "two", ones, ones, false);
	soundManager->playStreaming("sound/theme.ogg", 1, "one", twos, threes, true);
	soundManager->playStreaming("sound/music.ogg", 2, "two", ones, ones, false);
	sound3s = soundManager->playSound(sound3b, 3, "three", twos, twos, true);
	/**for (int i = 0; i < 10*delay; i++)
		soundManager->update();
	for (int i = 0; i < 5*delay; i++)
		printf("pause");
	sound3s = soundManager->playSound(sound3b, 10, "three", twos, twos, true);
	for (int i = 0; i < 5*delay; i++)
		printf("pause");**/
/**	for (;;)
		soundManager->update();

	soundManager->playMusic();
	for (int i = 0; i < 5* delay; i++)
		soundManager->update();
	soundManager->stopMusic();
	for (int i = 0; i < delay; i++)
		printf("stop\n");
	soundManager->playMusic();
	for (int i = 0; i < 10 * delay; i++)
		soundManager->update();
	soundManager->pauseMusic();
	for (int i = 0; i < delay; i++)
		printf("pasue\n");
	soundManager->resumeMusic();
	for (int i = 0; i < 10 * delay; i++)
		soundManager->update();
**/

	ALubyte c = ' ';

	while (c != 'q')
	{
		c = getche();

		switch (c)
		{
			// Pressing 'p' will begin playing the sample.
			case 'p':
				soundManager->playMusic();
				break;

			// Pressing 's' will stop the sample from playing.
			case 's':
				soundManager->stopMusic();
				break;

			// Pressing 'h' will pause (hold) the sample.
			case 'h':
				soundManager->pauseMusic();
				break;
			case 'r':
				soundManager->resumeMusic();
				break;
			case '1':
				sound1s = soundManager->playSound(sound1b, 1, "one", twos, twos, false);
				break;
			case '2':
				sound2s = soundManager->playSound(sound2b, 2, "two", ones, ones, false);
				break;
			case '3':
				sound3s = soundManager->playSound(sound3b, 3, "three", twos, twos, true);
				break;
			case '4':
				if (sound3s)
					sound3s->pause();
				break;
			case '5':
				if (sound3s)
					sound3s->resume();
				break;
			case '6':
				if (sound3s)
					sound3s->stop();
				break;
		};

		//soundManager->update();
	}

	printf("\nexit\n");
	delete soundManager;

	system("pause");
	return 0;
}